<template>
	<view class="ys">
		<view class="form">
			<uni-section title="手机号" type="line" v-if="id==''">
				<view class="inputbox">
					<uni-easyinput v-model="phone" trim placeholder="请输入手机号" clearSize="20"></uni-easyinput>
				</view>
			</uni-section>
			<uni-section title="姓名" type="line">
				<view class="inputbox">
					<uni-easyinput v-model="name" trim placeholder="请输入姓名" clearSize="20"></uni-easyinput>
				</view>
			</uni-section>
			<uni-section title="身份" type="line">
				<view class="inputbox">
					<uni-data-checkbox v-model="role" :localdata="rolelist"></uni-data-checkbox>
				</view>
			</uni-section>
			<uni-section title="组别" type="line" v-if="role == 2">
				<view class="inputbox">
					<uni-data-select v-model="group_id" :localdata="groupRange"></uni-data-select>
				</view>
			</uni-section>
			<uni-section title="组长" type="line" v-if="role == 2">
				<view class="inputbox">
					<uni-data-checkbox v-model="group_leader" :localdata="zzlist"></uni-data-checkbox>
				</view>
			</uni-section>
			<!-- <uni-section title="权限" type="line" v-if="role == 3">
				<view class="inputbox">
					<uni-data-checkbox multiple v-model="qx_id" :localdata="qxlist"></uni-data-checkbox>
				</view>
			</uni-section> -->
		</view>

		<button class="btn" @click="addStoreStaff" :loading="loading" :disabled="loading">提交</button>
	</view>
</template>

<script>
	import {
		staffDetail,
		addStoreStaff,
		getStoreGroup
	} from "@/api/user.js"
	import {
		commonParam,
		getMenu
	} from "@/api/api.js"
	export default {
		data() {
			return {
				name: '',
				phone: '',
				rolelist: [{
					text: '主管',
					value: 1

				}, {
					text: '促销员',
					value: 2
				}],
				zzlist: [{
					text: '是',
					value: 1

				}, {
					text: '否',
					value: 0
				}],
				group_leader: 0,
				loading: false,
				role: '',
				group_id: '',
				groupRange: [],
				qx_id: [],
				qxlist: [],
				id: ''
			}
		},
		onLoad(e) {
			if (e.id) {
				this.id = e.id
				uni.setNavigationBarTitle({
					title: '编辑员工'
				})
				this.staffDetail()
			}
			this.commonParam()
			this.getStoreGroup()
			this.getMenu()
		},
		methods: {
			staffDetail() {
				staffDetail({
					id: this.id
				}).then(res => {
					if (res.code == 0) {
						let data = res.data
						this.phone = data.phone
						this.name = data.name
						this.group_id = data.group_id
						this.group_leader = data.group_leader
						this.role = data.role.value
						this.qx_id = data.menu.map(item => {
							return item.menu_id
						})
					}
				})
			},
			getMenu() {
				getMenu().then(res => {
					if (res.code == 0) {
						let arr = res.data
						arr.forEach(item => {
							this.qxlist.push({
								text: item.name,
								value: item.id
							})
						})
					}
				})
			},
			getStoreGroup() {
				getStoreGroup().then(res => {
					if (res.code == 0) {
						let data = res.data
						data.forEach(item => {
							this.groupRange.push({
								text: item.name,
								value: item.id
							})
						})
					}
				})
			},
			commonParam() {
				commonParam().then(res => {
					if (res.code == 0) {
						this.rolelist = Object.values(res.data.staffRole)
					}
				})
			},
			addStoreStaff() {
				let that = this
				if (this.phone == '') {
					return uni.$showMsg('请输入手机号')
				}
				if (this.name == '') {
					return uni.$showMsg('请输入姓名')
				}
				if (this.role == '') {
					return uni.$showMsg('请选择身份')
				}
				that.loading = true
				addStoreStaff({
					id: this.id,
					name: this.name,
					phone: this.phone,
					role: this.role,
					group_id: this.group_id,
					group_leader: this.group_leader,
					menu_ids: this.qx_id
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						that.loading = false
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.ys {
		.inputbox {
			width: 96%;
			margin: 0px auto;
		}

		.form {
			padding-bottom: 15px;
			background: #fff;
		}



		.btn {
			button {
				color: #fff;
				font-size: 16px;
				letter-spacing: 1px;
				margin: 30px auto;
				width: 96%;
			}
		}
	}
</style>